import { defineComponent, reactive, toRefs } from 'vue'
import { NInput, NForm, NFormItem, NModal, FormRules } from 'naive-ui'
import { Response } from '@/service/modules/common/types'
import { editDepartmentReq } from '@/service/modules/department'

const EditDepartmentModal = defineComponent({
  name: 'EditDepartmentModal',
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    department: {
      type: Object,
      required: true,
      default: {}
    }
  },
  emits: ['modalShowChange', 'refreshTree'],
  setup() {
    const state = reactive({
      departmentForm: {
        departmentId: 0,
        newName: ''
      },
      positiveButtonDisabled: false,
      rules: {
        name: {
          trigger: ['input', 'blur'],
          required: true,
          validator() {
            if (state.departmentForm.newName === '') {
              return new Error(window.$t('department.nameTips'))
            }
          }
        }
      } as FormRules
    })
    return { ...toRefs(state) }
  },
  watch: {
    //给表单赋值
    showModal() {
      this.departmentForm.departmentId = this.department.id
      this.departmentForm.newName = this.department.name
      this.positiveButtonDisabled = false
    }
  },
  methods: {
    async onPositiveClick() {
      //输入框存在为空时则确认按钮无效
      if (this.departmentForm.newName === '') {
        return
      }
      //提交添加菜单请求
      this.positiveButtonDisabled = true
      const res: Response<any> = await editDepartmentReq(
        this.departmentForm
      ).catch(() => {
        this.positiveButtonDisabled = false
      })
      if (res.resp_code === 0) {
        this.initData()
        this.$emit('modalShowChange', false)
        this.$emit('refreshTree')
      } else {
        this.positiveButtonDisabled = false
      }
    },
    onNegativeClick() {
      this.initData()
      this.$emit('modalShowChange', false)
    },
    initData() {
      this.departmentForm.departmentId = 0
      this.departmentForm.newName = ''
    }
  },
  render() {
    return (
      <NModal
        show={this.showModal}
        mask-closable={false}
        style='width: 400px'
        title={window.$t('department.editDepartment')}
        bordered={false}
        size='huge'
        preset='dialog'
        positive-text={window.$t('dialog.positiveText')}
        negative-text={window.$t('dialog.negativeText')}
        onPositiveClick={this.onPositiveClick}
        onNegativeClick={this.onNegativeClick}
        positiveButtonProps={{
          disabled:
            this.positiveButtonDisabled || this.departmentForm.newName === ''
        }}
        onClose={this.onNegativeClick}
      >
        <NForm
          rules={this.rules}
          label-placement='left'
          require-mark-placement='right-hanging'
          label-width='auto'
        >
          <NFormItem path='name' label={window.$t('department.name')}>
            <NInput
              type='text'
              v-model={[this.departmentForm.newName, 'value']}
              placeholder={window.$t('department.nameTips')}
              autofocus
            />
          </NFormItem>
        </NForm>
      </NModal>
    )
  }
})

export default EditDepartmentModal
